tArgs = {...}

OneOS.LoadAPI('/System/API/Drawing.lua')
OneOS.LoadAPI('/System/API/ProgressBar.lua')
OneOS.LoadAPI('/System/API/Helpers.lua')

local canClose = false

fs = OneOS.FS

OneOS.CanClose = function()
	return canClose
end

Current = {
	Clicks = {},
	StatusText = 'Connecting',
	SecondaryStatusText = ''
}

Events = {
	
}

InterfaceElements = {
	
}


function Initialise()
	logo = Drawing.LoadImage('/System/Images/Logo')

	progressBar = ProgressBar:Initialise(math.floor((Drawing.Screen.Width - 40) / 2), math.floor(Drawing.Screen.Height / 2) - 2, 40, 1, colours.lightGrey, colours.blue, nil, ' ', 0, 0):Register()

	Draw()
	sleep(0)

	updaterEnvironment = {}
	setmetatable( updaterEnvironment, { __index = getfenv() } )
	local fnAPI, err = OneOS.LoadFile('/System/update.lua')
	if fnAPI then
		setfenv( fnAPI, updaterEnvironment)
		local ok, err = pcall(fnAPI, UpdaterRefresh)
		if not ok then

		end
	end
	UpdaterRefresh()
	canClose = true
	if updaterEnvironment.Settings.TotalFiles == updaterEnvironment.Settings.DownloadedFiles then
		sleep(3)
		OneOS.Reboot()
	else
		Current.StatusText = 'Download Failed!'
		Current.SecondaryStatusText = updaterEnvironment.Settings.TotalFiles - updaterEnvironment.Settings.DownloadedFiles .. ' files were not downloaded. Try again.'
	end
end

function UpdaterRefresh()
	progressBar.Total = updaterEnvironment.Settings.TotalBytes
	progressBar.Value = updaterEnvironment.Settings.DownloadedBytes
	if progressBar.Value == progressBar.Total and progressBar.Value ~= 0 then
		Current.StatusText = 'Download Complete!'
		Current.SecondaryStatusText = 'Your computer will restart in 3 seconds...'
	elseif progressBar.Value ~= 0 then
		progressBar.Text = ''
		Current.StatusText = 'Downloading Files...'
		Current.SecondaryStatusText = FormatBytes(updaterEnvironment.Settings.TotalBytes - updaterEnvironment.Settings.DownloadedBytes) .. ' ('..updaterEnvironment.Settings.TotalFiles - updaterEnvironment.Settings.DownloadedFiles..' files) Remaining...'
	else
		Current.StatusText = 'Preparing Download'--updaterEnvironment.Settings.Status
		Current.SecondaryStatusText = updaterEnvironment.Settings.SecondaryStatus
	end
	Draw()
end

function Draw()
	Drawing.Clear(colours.white)
	Drawing.DrawCharactersCenter(nil, -5, nil, nil, 'Updating OneOS', colours.blue, colours.white)
	for i, elem in ipairs(InterfaceElements) do
		if elem.Draw then
			elem:Draw()
		end
	end

	if Current.StatusText then
		Drawing.DrawCharactersCenter(nil, nil, nil, nil, Current.StatusText, colours.blue, colours.white)
		if Current.SecondaryStatusText then
			Drawing.DrawCharactersCenter(nil, 1, nil, nil, Current.SecondaryStatusText, colours.grey, colours.white)
		end
	end

	Drawing.DrawBuffer()
end

function FormatBytes(bytes)
	if bytes < 1024 then
		return "< 1KB"
	elseif bytes < 1024 * 1024 then
		return math.ceil(bytes / 1024) .. 'KB'
	elseif bytes < 1024 * 1024 * 1024 then
		--string.format('%.2f', ...) wasn't working for some reason
		local b = math.ceil((bytes / 1024 / 1024)*100)
		return b/100 .. 'MB'
	else
		return '> 1GB'
	end
end

MainDraw = Draw

function RegisterElement(elem)
	table.insert(InterfaceElements, elem)
end

function UnregisterElement(elem)
	for i, e in ipairs(InterfaceElements) do
		if elem == e then
			InterfaceElements[i] = nil
		end
	end
end

function GetAbsolutePosition(obj)
	if not obj.Parent then
		return {X = obj.X, Y = obj.Y}
	else
		local pos = GetAbsolutePosition(obj.Parent)
		local x = pos.X + obj.X - 1
		local y = pos.Y + obj.Y - 1
		return {X = x, Y = y}
	end
end

Initialise()